//
//  UpdateProgressView.m
//  MiBaoTu
//
//  Created by admin on 16/9/20.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "UpdateProgressView.h"

@interface UpdateProgressView () {
    UILabel         *_progressLb;
    UIButton        *_upDateBt;
    UIBezierPath    *_showBezierPath;
    CAShapeLayer    *_showLayer;
}

@end
@implementation UpdateProgressView

- (id)init
{
    self = [super init];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.frame = CGRectMake((SCREEN_WIDTH-400*PERCEN)/2., (SCREEN_HEIGHT-300*PERCEN)/2., 400*PERCEN, 300*PERCEN);
    self.backgroundColor = [UIColorFromHex(0x000000)colorWithAlphaComponent:.6];
    self.layer.cornerRadius = 5.f;
    self.layer.masksToBounds = YES;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width/2., 120*PERCEN) radius:60*PERCEN startAngle:0 endAngle:M_PI*2 clockwise:YES];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineWidth = 1;
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:shapeLayer];
    
    _showBezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width/2., 120*PERCEN) radius:60*PERCEN startAngle:-M_PI_2 endAngle:M_PI_2+M_PI clockwise:YES];
    _showLayer = [CAShapeLayer layer];
    _showLayer.lineWidth = 1;
    _showLayer.path = _showBezierPath.CGPath;
    _showLayer.strokeColor = TINK_BG_COLOR.CGColor;
    _showLayer.fillColor = [UIColor clearColor].CGColor;
    _showLayer.strokeStart = 0;
    _showLayer.strokeEnd = 0;
    [self.layer addSublayer:_showLayer];
    
    _progressLb = [[UILabel alloc]initWithFrame:CGRectMake(self.width/2.-50*PERCEN, 120*PERCEN-10, 100*PERCEN, 20)];
    _progressLb.backgroundColor = [UIColor clearColor];
    _progressLb.textAlignment = NSTextAlignmentCenter;
    _progressLb.textColor = UIColorFromHex(0xffffff);
    _progressLb.font = [UIFont systemFontOfSize:13.f];
    _progressLb.text = @"0%";
    [self addSubview:_progressLb];
    
    _upDateBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _upDateBt.frame = CGRectMake(0, 200*PERCEN, self.width, 20);
    _upDateBt.bottom = self.height - 50*PERCEN;
    [_upDateBt setTitle:@"上传中..." forState:UIControlStateNormal];
    [_upDateBt setTitle:@"上传中..." forState:UIControlStateHighlighted];
    [_upDateBt setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateNormal];
    [_upDateBt setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateHighlighted];
    _upDateBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [_upDateBt addTarget:self action:@selector(onUpdateAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_upDateBt];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    self.center = CGPointMake(SCREEN_WIDTH/2., SCREEN_HEIGHT/2.);
}

- (void)showInView:(UIView *)view
{
    self.center = view.center;
    [view addSubview:self];
}

- (void)onUpdateAction
{
    [self removeFromSuperview];
    if (_delegate && [_delegate respondsToSelector:@selector(onCompleteUpdateProgeressView:)]) {
        [_delegate onCompleteUpdateProgeressView:self];
    }
}

- (void)setProgress:(int)progress
{
    _progress = progress;
  
    [self performSelectorOnMainThread:@selector(onUpdateUI) withObject:nil waitUntilDone:YES];
}

- (void)onShowFaileContent:(UpdateProgressViewType)typeShow
{
    self.type = typeShow;
    if (typeShow == UpdateProgressViewType_Faile) {
        [_upDateBt setTitle:@"上传失败" forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传失败" forState:UIControlStateHighlighted];
        _upDateBt.hidden = NO;
    } else if (typeShow == UpdateProgressViewType_Success) {
        [_upDateBt setTitle:@"上传完成" forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传完成" forState:UIControlStateHighlighted];
        _upDateBt.hidden = NO;
    } else {
        [_upDateBt setTitle:@"上传中..." forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传中..." forState:UIControlStateHighlighted];
    }
    
    if (typeShow == UpdateProgressViewType_Faile || typeShow == UpdateProgressViewType_Success) {
        [self performSelector:@selector(onUpdateAction) withObject:nil afterDelay:.1];
    }
}

- (void)onUpdateUI
{
    _progressLb.text = [NSString stringWithFormat:@"%d%%",_progress];
    
    _showLayer.strokeStart = 0;
    _showLayer.strokeEnd = _progress/100.0;
    
    if (_progress > 0) {
        [_upDateBt setTitle:@"上传中..." forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传中..." forState:UIControlStateHighlighted];
    }
    else if (_progress >= 100) {
        [_upDateBt setTitle:@"上传完成" forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传完成" forState:UIControlStateHighlighted];
    }
    if (self.type == UpdateProgressViewType_Faile) {
        [_upDateBt setTitle:@"上传失败" forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传失败" forState:UIControlStateHighlighted];
        _upDateBt.hidden = NO;
    } else if (self.type == UpdateProgressViewType_Success) {
        [_upDateBt setTitle:@"上传完成" forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传完成" forState:UIControlStateHighlighted];
        _upDateBt.hidden = NO;
    } else {
        [_upDateBt setTitle:@"上传中..." forState:UIControlStateNormal];
        [_upDateBt setTitle:@"上传中..." forState:UIControlStateHighlighted];
    }

}

@end
